package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.ResponseResult;
import com.lagou.domain.Role;
import com.lagou.domain.User;
import com.lagou.domain.UserVo;
import com.lagou.service.UserMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapperService userMapperService;

    /**
     * 用户分页 & 条件查询
     * @param userVo
     * @return
     */
    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVo userVo) {
        PageInfo<User> pageInfo = userMapperService.findAllUserByPage(userVo);
        return new ResponseResult(true,200,"查询成功",pageInfo);
    }

    /**
     * 用户状态设置
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(Integer id, String status) {
        userMapperService.updateUserStatus(id, status);
        return new ResponseResult(true,200,"修改状态成功",status);
    }

    /*
    用户登录
     */
    @RequestMapping("/login")
    public ResponseResult login(User user, HttpServletRequest request) throws Exception {
        User login = userMapperService.login(user);
        if (login != null) {
            HttpSession session = request.getSession();
            String access_token = UUID.randomUUID().toString();
            session.setAttribute("access_token", access_token);
            session.setAttribute("user_id", login.getId());
            Map<String, Object> map = new HashMap<>();
            map.put("access_token", access_token);
            map.put("user_id", login.getId());
            return new ResponseResult(true,200,"登陆成功",map);
        } else {
            return new ResponseResult(false,400,"登陆失败，用户名或密码错误",null);
        }
    }

    /**
     * 查询用户关联的角色账户
     * @param id
     * @return
     */
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(Integer id) {
        List<Role> list = userMapperService.findUserRelationRoleById(id);
        return new ResponseResult(true,200,"查询成功",list);
    }

    /**
     * 为用户添加角色
     * @param userVo
     * @return
     */
    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVo userVo) {
        userMapperService.userContextRole(userVo);
        return new ResponseResult(true,200,"添加角色成功",null);
    }

    /*
   获取用户权限，进行菜单动态展示
    */
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request) {
        //获取请求头中的token
        String header_token = request.getHeader("Authorization");

        //获取session中的token
        String session_token = (String) request.getSession().getAttribute("access_token");

        //判断是否一致
        if (header_token.equals(session_token)) {
            //获取用户id
            Integer user_id = (Integer) request.getSession().getAttribute("user_id");
            return userMapperService.getUserPermissions(user_id);
        } else {
            return new ResponseResult(false,400,"获取菜单信息失败",null);
        }
    }
}
